#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_TARGET_map)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/map.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "map.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

// #define EXIT_TEST
extern uint64_t unitt_clock(void);

static int test_0(void)
{
    for (int i = 0; i < 100; i++)
    {
        if (0) 
        {
            
            #if defined (EXIT_TEST)
            exit(0);
            #endif 
            return UNITT_E_FAIL;
        }
    }
    
    return UNITT_E_OK;
}

static void unitt_task(void)
{
    static UNITT_TCASE rand_tests[] = {
        UNITT_TCASE(test_0),
        // UNITT_TCASE(test_1),
        // UNITT_TCASE(test_2),
    };

    static UNITT suites[] = {
        { "xxx suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

static void test_create(void)
{
    map_t map_char = map(char, int);
    map_t map_int = map(int, int);
    map_t map_float = map(float, int);
    map_t map_double = map(double, int);
    map_t map_string = map(string, int);

    _map(map_char);
    _map(map_int);
    _map(map_float);
    _map(map_double);
    _map(map_string);
}

static void test_insert(void)
{
    int value;
    map_t map = map(string, int);
    if (!map) return;

    value = 100; map_insert(map, mpair("hello", &value));
    value = 110; map_insert(map, mpair("Zhang", &value));

    printf("map[hello] = %d\r\n", map_at(map, int, "hello"));
    printf("map[Zhang] = %d\r\n", map_at(map, int, "Zhang"));
    map_erase(map, "hello");

    _map(map);
}

static void test_at(void)
{
    map_t map = map(string, int);
    int value;

    value = 100; map_insert(map, mpair("hello", &value));
    value = 1; map_insert(map, mpair("ZhangSan", &value));
    value = 2; map_insert(map, mpair("LiSi", &value));
    value = 3; map_insert(map, mpair("WangWu", &value));
    value = 4; map_insert(map, mpair("SunLiu", &value));
    value = 5; map_insert(map, mpair("QianQi", &value));

    printf("map[%s] = %d\r\n", "hello", map_at(map, int, "hello"));
    printf("map[%s] = %d\r\n", "ZhangSan", map_at(map, int, "ZhangSan"));
    printf("map[%s] = %d\r\n", "LiSi", map_at(map, int, "LiSi"));
    printf("map[%s] = %d\r\n", "WangWu", map_at(map, int, "WangWu"));
    printf("map[%s] = %d\r\n", "SunLiu", map_at(map, int, "SunLiu"));
    printf("map[%s] = %d\r\n", "QianQi", map_at(map, int, "QianQi"));

    _map(map);
}

static void test_find(void)
{
    map_t map = map(string, int);
    int value;

    value = 100; map_insert(map, mpair("hello", &value));
    value = 1; map_insert(map, mpair("ZhangSan", &value));
    value = 2; map_insert(map, mpair("LiSi", &value));
    value = 3; map_insert(map, mpair("WangWu", &value));
    value = 4; map_insert(map, mpair("SunLiu", &value));
    value = 5; map_insert(map, mpair("QianQi", &value));

    printf("find ""hello"" %d\r\n", map_find(map, "hello"));
    printf("find ""lllll"" %d\r\n", map_find(map, "lllll"));

    _map(map);
}

static void test_size(void)
{
    map_t map = map(string, int);
    int value;

    value = 100; map_insert(map, mpair("hello", &value));
    value = 1; map_insert(map, mpair("ZhangSan", &value));
    value = 2; map_insert(map, mpair("LiSi", &value));
    value = 3; map_insert(map, mpair("WangWu", &value));
    value = 4; map_insert(map, mpair("SunLiu", &value));
    value = 5; map_insert(map, mpair("QianQi", &value));

    printf("size = %d, key size = %d, value size = %d\r\n", map_size(map), map_ksize(map), map_vsize(map));

    _map(map);
}

static void test_it(void)
{
    map_t map = map(string, int);
    int value;
    char *key;
    void *data;
    int i;

    value = 100; map_insert(map, mpair("hello", &value));
    value = 1; map_insert(map, mpair("ZhangSan", &value));
    value = 2; map_insert(map, mpair("LiSi", &value));
    value = 3; map_insert(map, mpair("WangWu", &value));
    value = 4; map_insert(map, mpair("SunLiu", &value));
    value = 5; map_insert(map, mpair("QianQi", &value));

    map_it_init(map, MAP_HEAD);
    i = map_size(map);
    while (i--)
    {
        data = map_it_get(map, &key, NULL);
        printf("map[%s] = %d\r\n", key, *(int *)data);
    }

    _map(map);
}

static void test_float_map(void)
{
    int value;
    map_t map = map(double, int);
    if (!map) return;

    value = 111; map_insert(map, mpair(12.3, &value));
    value = 100; map_insert(map, mpair(16.9, &value));
    printf("%d\r\n", map_at(map, int, 16.9000000000000000000001));
    map_erase(map, 16.9);
    _map(map);
}

static void test_base(void)
{
    test_it();
    // test_float_map();
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: map [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <ut>        Unit test\n"
"                        <create>    Test create and delete functions\n"
"                        <insert>    Test insert and erase function\n"
"                        <at>        Test get data function\n"
"                        <find>      Test find function\n"
"                        <size>      Test sizes function\n"
"                        <it>        Test iterator function\n"
"                        <other>     Test other key types function\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -u [<period>]       Unit test period, unit ms, the default is 1000ms\n"
"\n"

    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int ut_period = 1000;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'u' :
            if (command_optarg) ut_period = atoi(command_optarg);
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("map version %d.%d.%d\r\n", MAP_V_MAJOR, MAP_V_MINOR, MAP_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "ut"))
        {
            srand((unsigned int)time(NULL));
            #if defined(TEST_TARGET_map)
            while (1)
            {
                unitt_task();
                usleep(1000 * ut_period);
            }
            #else  
            printf("create task %d\r\n", task_create(ut_period, unitt_task));
            #endif
        }
        else if (!strcmp(execute, "create"))
        {
            test_create();
        }
        else if (!strcmp(execute, "insert"))
        {
            test_insert();
        }
        else if (!strcmp(execute, "at"))
        {
            test_at();
        }
        else if (!strcmp(execute, "find"))
        {
            test_find();
        }
        else if (!strcmp(execute, "size"))
        {
            test_size();
        }
        else if (!strcmp(execute, "it"))
        {
            test_it();
        }
        else if (!strcmp(execute, "other"))
        {
            test_float_map();
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_map)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_map(void)
{
    command_export("map", test);
}
init_export_app(test_map);
#endif 

